package com.zhang.common.captcha;


import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import com.zhang.common.util.redis.RedisUtil;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Slf4j
@RestController
public class CaptchaController {

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/getCaptcha")
    public R captcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为2分钟
        redisUtil.set(key, verCode, 1, TimeUnit.MINUTES);
        HashMap<String, String> map = new HashMap<>();
        map.put("key",key );
        map.put("image", specCaptcha.toBase64());
        log.info("验证码key为：code:{},验证码值为：value:{}",key,verCode);
        // 将key和base64返回给前端
        return RUtils.successData(map);
    }
}
